var Renyuanfenpei = new Vue({

    el : '#Renyuanfenpeilist',

    data:{
        xiangmuId:'',
        usernametext:'',
        renyuanlist:[],
        fenpeirenyuanlist:[],
        xiangmu : {},
        showrenyuan : false,					// 显示列表是否有数据
        showfenpeirenyuan: false,
    },
    created : function() {
        $(".nav navbar-nav li").removeClass("active");
        $("#nav_renyuanfenpei").addClass("active");
        this.findIsacticed();

    },
    methods : {
        toshowUploadXls : function(ppxiangmuid){
            this.xiangmuId=ppxiangmuid;
            $("#uploadModal").modal("show");
            this.getFileUrl("ylsbtp",'2000','xls,xlsx');
        },
        getFileUrl : function(ppFolderName,ppFileSize,ppShangChuanWenJianLeiXing){
            var _this = this;
            $("#uploadModal-body").load("upload_batch.html?fujianpath=/ylsbtp/"+ppFolderName+"&filesize="+ppFileSize, function(){
                UploadVue.Init(ppShangChuanWenJianLeiXing,ppFileSize+"kb",function(jsonList){
                    if(jsonList.length==0){
                        return;
                    }
                    var filename=jsonList[0].filename;
                    var fileurl=jsonList[0].fileurl;
                    var filesize=jsonList[0].filesize;
                     _this.getValue(fileurl);


                },function(){
                    $("#uploadModal").modal("hide");
                });
            });
        },
        getValue : function(ppFileurl){
            var _this=this;
            _this.excelxiangmulist=[];
            layer.open({type:3});
            $.post("/import_xiangmu/getvalue",{
                xianmguid : _this.xiangmuId,
                fileurl:ppFileurl,
                random : Math.random()
            },function(ppData){
                layer.closeAll("loading");
                if(ppData != null){
                    var mmData = ppData;
                    var result = mmData.result;
                    var message = mmData.message;
                    var data = mmData.resultContent;
                    if(result == "1") {
                        _this.excelxiangmulist = ppData.resultContent;
                        _this.import();

                    }else{
                        layer.alert(message);
                    }
                }
            },"json");

        },
        import : function(){
            var _this = this;
            layer.confirm("确定要导入吗？",{
                btn : ["是","否"]
            },function(){
                layer.closeAll("dialog");
                _this.startDaoru();
            });
        },

        startDaoru : function(){
            var _this = this;
            layer.open({type:3});
            _this.daoru(0);
        },
        daoru : function(ppMuluCurrentIndex){
            layer.open({type:3});
            var _this=this;
            var  CurrentIndex = ppMuluCurrentIndex+1;
            layer.open({
                type: 3,
                content: "<div style='font-size:18px;font-weight:bold;padding-top:40px;width:200px;text-align:left;'>正在导入项目<br/>当前进度    "+CurrentIndex+" / "+_this.excelxiangmulist.length+"</div>"
            });

            $.post("/import_xiangmu/import",{
                xianmguid : _this.xiangmuId,
                xuhao:_this.excelxiangmulist[ppMuluCurrentIndex].序号,
                username:_this.excelxiangmulist[ppMuluCurrentIndex].姓名,
                buzhibie:_this.excelxiangmulist[ppMuluCurrentIndex].部职别,
                account:_this.excelxiangmulist[ppMuluCurrentIndex].账号,
                password:_this.excelxiangmulist[ppMuluCurrentIndex].密码,
                random : Math.random()
            }, function(ppData) {
                layer.closeAll("loading");

                if (ppData.result == "0") {
                    layer.alert(message);
                }else{

                    if(ppMuluCurrentIndex >=_this.excelxiangmulist.length - 1){

                        layer.alert("导入完成！");
                        _this.findRenyuanFenpeiByXiangmuId();
                        $("#uploadModal").modal("hide");
                    }else{
                        ppMuluCurrentIndex++;
                        _this.daoru(ppMuluCurrentIndex);
                    }
                }
            },"json")
        },

        findIsacticed:function (){
            var _this = this;
            layer.open({type:3});
            $.post('/xiangmu/getIsactivedxiangmu', {
                rdm : Math.random()
            },function(ppData) {
                layer.closeAll("loading");
                if(ppData != null){
                    if(ppData.result == "1"){
                        var data = ppData.resultContent;
                        _this.xiangmu = data;
                        _this.xiangmuId=_this.xiangmu.xiangmuid;
                        _this.findRenyuanFenpeiByXiangmuId();

                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },


        bindRenyuanList:function (){
            var _this = this;
            if(""==_this.usernametext){
                layer.alert("请输入姓名");
                return false;
            }
            _this.showrenyuan=false;
            _this.renyuanlist=[];
            layer.open({type:3});
            $.post("/user/findvaild",{
                username :_this.usernametext,
                pageindex : 1,
                pagesize : 100,
                rdm : Math.random()
            },function(ppData){
                layer.closeAll("loading");
                if (ppData != null){
                    if(ppData.result == "1"){
                        var data=ppData.resultContent;
                        if(data.RenYuanList.length > 0){
                            _this.showrenyuan = true;
                            _this.renyuanlist=data.RenYuanList;
                        }else {
                            _this.showrenyuan = false;
                        }

                    }else{
                        layer.alert(ppData.message);
                    }
                }

            },"json");
        },

        findRenyuanFenpeiByXiangmuId:function (){
            var _this = this;
            layer.open({type:3});
            $.post("/renyuanfenpei/findRenyuanFenpeiByXiangmuId",{
                xiangmuid :_this.xiangmuId,
                rdm : Math.random()
            },function(ppData){
                layer.closeAll("loading");
                if (ppData != null){
                    if(ppData.result == "1"){
                        var data=ppData.resultContent;

                        if(data.length > 0){
                            _this.showfenpeirenyuan = true;
                            _this.fenpeirenyuanlist=data;
                        }else {
                            _this.showfenpeirenyuan = false;
                        }

                    }else{
                        layer.alert(ppData.message);
                    }
                }

            },"json");
        },

        toAddfenpei : function (ppuserId){
                var _this = this;

                layer.open({type:3});
                $.post('/renyuanfenpei/add',{
                    xiangmuid:_this.xiangmuId,
                    userid:ppuserId,
                    random : Math.random()
                },function(ppData){
                    if(ppData != null){
                        layer.closeAll("loading");

                        if(ppData.result == "1"){
                            layer.open({
                                time:1000,
                                btn:[],
                                content:"加入成功!",
                            });

                            _this.findRenyuanFenpeiByXiangmuId();
                        }else{
                            layer.alert(ppData.message);
                        }
                    }
                },"json");
        },

        toDelete:function (ppId){
            var _this = this;

            layer.confirm("确定将改人员从当前项目中删除吗？",{
                btn : ['是','否']
            },function(){
                layer.open({type:3});

                $.post("/renyuanfenpei/delete", {
                    id : ppId,
                    random : Math.random()
                }, function(ppData) {
                    if (ppData != null) {
                        layer.closeAll("loading");

                        if(ppData.result != "1"){
                            layer.alert(ppData.message);
                        }else{
                            layer.open({
                                time:1000,
                                btn:[],
                                content:"删除成功!",
                            });

                            _this.findRenyuanFenpeiByXiangmuId();
                        }
                    }
                },"json");
            })
        },


    },


})